const fs = require('fs')
const path = require('path')
const dayjs = require('dayjs')
 //获取新闻列表
function news(){
    const fullPath = path.join(__dirname,'../db/news.json')
    const res = fs.readFileSync(fullPath)
    obj = JSON.parse(res)
    obj.forEach(e => {
        e.publish_time = dayjs().format('YYYY-MM-DD HH:mm:ss')
    });
    return obj
}
//获取学生列表
function students(){
    const fullPath = path.join(__dirname,'../db/info.json')
    const res =fs.readFileSync(fullPath)
    const obj = JSON.parse(res)
    return obj
}
//获取幸运儿
function luckystar(){
    const fullPath = path.join(__dirname,'../db/info.json')
    const res =fs.readFileSync(fullPath)
    const obj = JSON.parse(res)
    const randomNum = parseInt(Math.random()*(obj.length))
    const luckystar = obj[randomNum]
    luckystar.count++
    const json = JSON.stringify(obj)
    fs.writeFileSync(fullPath,json)
    return luckystar
}
module.exports={
    news,
    students,
    luckystar,
}